package madixin.t66;

import java.util.*;

class Solution {
    public int[] plusOne(int[] digits) {
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] == 9) {
                digits[i] = 0;//缝9进1
            } else {
                digits[i]++;//非9的话，直接返回
                return digits;
            }

        }
        int[] res = new int[digits.length + 1];//补充进位
        res[0] = 1;
        System.arraycopy(digits,0,res,1,digits.length);
        return res;
    }


    public int[] plusOne1(int[] digits) {
        List<Integer> list = new ArrayList<>();
        int add = 0;
        for (int i = digits.length - 1; i >= 0; i--) {
            int cur = add + digits[i];
            if (i == digits.length - 1) {
                cur++;
            }
            add = cur / 10;
            cur = cur % 10;
            list.add(cur);
        }
        if (add == 1) {
            list.add(1);
        }

        int[] res = new int[list.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = list.get(res.length - i - 1);
        }
        return res;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(Arrays.toString(s.plusOne(new int[]{9, 9, 8})));
    }
}